module MyComparator16bit (
 input [15:0] A,
 input [15:0] B,
 output GT,
 output LT,
 output EQ
);
 wire gt_mid, lt_mid, eq_mid;

 MyComparator8bit upper (.A(A[15:8]), .B(B[15:8]), .GT_in(1'b0), .LT_in(1'b0), .EQ_in(1'b1),
								 .GT_out(gt_mid), .LT_out(lt_mid), .EQ_out(eq_mid));
 MyComparator8bit lower (.A(A[7:0]), .B(B[7:0]), .GT_in(gt_mid), .LT_in(lt_mid), .EQ_in(eq_mid),
								 .GT_out(GT), .LT_out(LT), .EQ_out(EQ));
endmodule


module MyComparator8bit (
 input [7:0] A,
 input [7:0] B,
 input GT_in,
 input LT_in,
 input EQ_in,
 output GT_out,
 output LT_out,
 output EQ_out
);
 wire gt_mid, lt_mid, eq_mid;

 MyComparator4bit upper (.A(A[7:4]), .B(B[7:4]), .GT_in(GT_in), .LT_in(LT_in), .EQ_in(EQ_in),
								 .GT_out(gt_mid), .LT_out(lt_mid), .EQ_out(eq_mid));
 MyComparator4bit lower (.A(A[3:0]), .B(B[3:0]), .GT_in(gt_mid), .LT_in(lt_mid), .EQ_in(eq_mid),
								 .GT_out(GT_out), .LT_out(LT_out), .EQ_out(EQ_out));
endmodule


module MyComparator4bit (
 input [3:0] A,
 input [3:0] B,
 input GT_in,
 input LT_in,
 input EQ_in,
 output GT_out,
 output LT_out,
 output EQ_out
);
 wire [2:0] gt_chain, lt_chain, eq_chain;

 MyComparator1bit comp3 (.A(A[3]), .B(B[3]), .GT_in(GT_in), .LT_in(LT_in), .EQ_in(EQ_in),
								 .GT_out(gt_chain[0]), .LT_out(lt_chain[0]), .EQ_out(eq_chain[0]));
 MyComparator1bit comp2 (.A(A[2]), .B(B[2]), .GT_in(gt_chain[0]), .LT_in(lt_chain[0]), .EQ_in(eq_chain[0]),
								 .GT_out(gt_chain[1]), .LT_out(lt_chain[1]), .EQ_out(eq_chain[1]));
 MyComparator1bit comp1 (.A(A[1]), .B(B[1]), .GT_in(gt_chain[1]), .LT_in(lt_chain[1]), .EQ_in(eq_chain[1]),
								 .GT_out(gt_chain[2]), .LT_out(lt_chain[2]), .EQ_out(eq_chain[2]));
 MyComparator1bit comp0 (.A(A[0]), .B(B[0]), .GT_in(gt_chain[2]), .LT_in(lt_chain[2]), .EQ_in(eq_chain[2]),
								 .GT_out(GT_out), .LT_out(LT_out), .EQ_out(EQ_out));
endmodule


module MyComparator1bit (
	input A,
	input B,
	input GT_in,
	input LT_in,
	input EQ_in,
	output GT_out,
	output LT_out,
	output EQ_out
);
	// GT_out = AB'E' + AC + B'C + CD + CE
	assign GT_out = (A & ~B & EQ_in) | GT_in;
	// LT_out = A'BC'E + D
	assign LT_out = (~A & B & ~GT_in & EQ_in) | LT_in;

	// EQ_out = A'B'E + A'DE + ABE + CDE
	assign EQ_out = (~A & ~B & EQ_in) | (~A & LT_in & EQ_in) | (A & B & EQ_in) | (GT_in & LT_in & EQ_in);
endmodule
